let logger = require("pomelo-logger").getLogger(__filename);
let CardUtil = require("./utils/CardUtil");
let ZhuojiGameConstant = require("./constant/zhuojiGameConstant");
let MahjongUtils = require("./utils/MahjongUtils");
let MahjongCheckResult = require("./domain/mahjongCheckResult");
let CardDown = require("./domain/cardDown");


class ZhuojiMahjongProcessor{

    static checkPeng(card, player){
        if (player.onclickTing || player.onclickTingChuPai) {
            return null;
        }
        if (player.haveCancelPengCards(card) || player.getXCardNumInHand(card) < 2) {
            return null;
        }

        let result = new MahjongCheckResult();
        result.uid = player.getUid();
        result.operation = ZhuojiGameConstant.MAHJONG_OPERTAION_PENG;
        result.targetCard = card;
        return result;
    }

    static checkTing(player, card){
        if (player.onclickTingChuPai) {
            return null;
        }
        let cards = [...player.cardsInHand];
        if(card !== 0) {
            cards.push(card);
        }
        let tingResult= MahjongUtils.checkTing14(cards);
        let tingCards = [...tingResult.keys()];
        if (tingCards.length > 0) {
            let tingResult = new MahjongCheckResult();
            tingResult.uid = player.getUid();
            tingResult.operation = ZhuojiGameConstant.MAHJONG_OPERTAION_TING;
            tingResult.targetCard = card;
            tingResult.tingList = tingCards;
            return tingResult;
        }
        return null;
    }

    static checkWin(card, player){
        if(player.haveCancelHuCard(card)) {
            return null;
        }
        let tmpCards = [...player.cardsInHand, card];
        let result = MahjongUtils.checkHu(tmpCards);
        logger.info(`---checkwin--- cards:${CardUtil.toHexString(tmpCards)},result:${result}`);
        if (result) {
            let mahjongCheckResult = new MahjongCheckResult();
            mahjongCheckResult.uid = player.getUid();
            mahjongCheckResult.operation = ZhuojiGameConstant.MAHJONG_OPERTAION_HU;
            mahjongCheckResult.targetCard = card;
            return mahjongCheckResult;
        }
        return null;
    }

    static checkGang(gameTable, newCard, player, isMyCard, chuUid){
        if (player.onclickTingChuPai || player.onclickTing) {
            return null;
        }
        if (gameTable.isEnd()) {
            return null;
        }
        let gangList = isMyCard ? player.getAnGangList() : [];
        let cardCountInHand = player.getXCardNumInHand(newCard);
        // if (cardCountInHand == 4) {
        //     addGangList(newCard, GameConstant.MAHJONG_OPERTAION_AN_GANG, ganglist);
        // }
        if (cardCountInHand == 3 && isMyCard) {
            let cardDown = new CardDown();
            cardDown.type = ZhuojiGameConstant.MAHJONG_OPERTAION_AN_GANG;
            cardDown.cards = [newCard, newCard, newCard, newCard];
            cardDown.chuPlayerUid = chuUid;
            cardDown.isHanBaoDou = 0;
            gangList.push(cardDown);
        }
        if (cardCountInHand == 3 && !isMyCard) {
            let cardDown = new CardDown();
            cardDown.type = ZhuojiGameConstant.MAHJONG_OPERTAION_MING_GANG;
            cardDown.cards = [newCard, newCard, newCard, newCard];
            cardDown.chuPlayerUid = chuUid;
            cardDown.isHanBaoDou = 0;
            gangList.push(cardDown);
        }
        if (player.hasPeng(newCard) && isMyCard) {
            let cardDown = new CardDown();
            cardDown.type = ZhuojiGameConstant.MAHJONG_OPERTAION_BU_GANG;
            cardDown.cards = [newCard, newCard, newCard, newCard];
            cardDown.chuPlayerUid = chuUid;
            cardDown.isHanBaoDou = 0;
            gangList.push(cardDown);
        }
        for(let card of player.cardsInHand){
            if (player.hasPeng(card) && isMyCard) {
                let cardDown = new CardDown();
                cardDown.type = ZhuojiGameConstant.MAHJONG_OPERTAION_BU_GANG;
                cardDown.cards = [card, card, card, newCard];
                cardDown.chuPlayerUid = chuUid;
                cardDown.isHanBaoDou = 1;
                gangList.push(cardDown);
            }
        }

        if(gangList.length > 0){
            let checkResult = new MahjongCheckResult();
            checkResult.uid = player.getUid();
            checkResult.operation = ZhuojiGameConstant.MAHJONG_OPERTAION_GANG;
            checkResult.targetCard = newCard;
            checkResult.gangList = gangList;
            return checkResult;
        }
        return null;
    }
}

module.exports = ZhuojiMahjongProcessor;